package com.example.myprojectoffeixue;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.apache.http.entity.StringEntity;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

public class ShareDeviceActivity extends Activity {
    ArrayList<SheBeiClass> all;
    ArrayList<String> flag;
    TextView set_finish;
    MyApplication m;
    adapter_Device kt;
    EditText mobile;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share_device);
        info();
    }


    @Override
    protected void onStart() {
        super.onStart();
    }

    public void info() {
        mobile = (EditText) findViewById(R.id.mobile);
        if (all == null) {
            all = new ArrayList<SheBeiClass>();
        } else {
            all.clear();
        }

        m = (MyApplication) getApplication();
        Log.d("lsb9size", m.lsb9.size() + "");
        ListView all_device = (ListView) findViewById(R.id.all_device);


        if (flag == null) {
            flag = new ArrayList<String>();
        } else {
            flag.clear();
            set_finish.setText("一键设置");
        }


        if (m.lsb9.size() != 0) {
            for (int i = 0; i < m.lsb9.size(); i++) {
                flag.add("0");

            }
        }
        kt = new adapter_Device(ShareDeviceActivity.this, flag);
        all_device.setAdapter(kt);

        LinearLayout tfinish = (LinearLayout) findViewById(R.id.iv_mykaiguan);
        tfinish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        set_finish = (TextView) findViewById(R.id.wancheng);

        set_finish.setOnClickListener(new View.OnClickListener() {



            @Override
            public void onClick(View v) {
                 set_finish.setClickable(false);
                 all.clear();
                for (int i = 0; i < flag.size(); i++) {

                    if ("1".equals(flag.get(i))) {
                        all.add(m.lsb9.get(i));
                    }
                }
                      if("".equals(mobile.getText().toString())){
                          set_finish.setClickable(true);
                          ToastUtils.showToast(getBaseContext(),"请输入被分享用户的手机号");

                      }else if(all.size()==0){
                          set_finish.setClickable(true);
                          ToastUtils.showToast(getBaseContext(),"请选择要分享的设备");

                      }else {
                    shardevice();}


            }
        });


    }

    public void shardevice() {
        UserClass u = new UserClass();
        u.type = "2";
        u.mobileFrom = m.username;
        u.mobileTo = mobile.getText().toString();
        u.authTime = "120";
        u.devices = all;
        String k = JSON.toJSONString(u);
        Log.d("cmd", k);
        k = AESUtil.encrypt(k);
        HttpUtils httpUtils = new HttpUtils();
        RequestParams params = new RequestParams();
        params.setContentType("application/json");
        try {
            params.setBodyEntity(new StringEntity(k, "utf-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        httpUtils.send(HttpRequest.HttpMethod.POST, HttpConnection.deviceAuthorized, params,
                new RequestCallBack<String>() {
                    @Override
                    public void onFailure(HttpException arg0, String arg1) {
                        set_finish.setClickable(true);
                        HttpConnection.toastLog("网络错误", ShareDeviceActivity.this);
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> arg0) {

                        set_finish.setClickable(true);
                        if (arg0.result != null && !("".equals(arg0.result))) {
                            arg0.result = AESUtil.decrypt(arg0.result);
                            Log.d("arg0",arg0.result);
                            JSONObject jb = JSONObject.parseObject(arg0.result);
                            String status = jb.getString("status");
                            if ("0".equals(status)) {
                                HttpConnection.toastLog("授权成功", ShareDeviceActivity.this);
                            } else if ("-2".equals(status)) {
                                HttpConnection.toastLog("请先注册", ShareDeviceActivity.this);
                            }else if("2".equals(status)){

                                HttpConnection.toastLog("存在相同的设备授权于该用户", ShareDeviceActivity.this);
                            }



                        } else {
                            HttpConnection.toastLog("服务器错误", ShareDeviceActivity.this);
                        }
                    }
                });
    }
}
